package com.bootdo.system.controller;

import com.bootdo.common.config.Constant;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.domain.AreaDO;
import com.bootdo.system.service.AreaService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域表
 * 
 * @author liangxiong
 * @email 1992lcg@163.com
 * @date 2018-06-29 09:39:36
 */
 
@Controller
@RequestMapping("/system/area")
public class AreaController {

	private String prefix = "system/area";

	@Autowired
	private AreaService areaService;

	/**
	 * 进入地区管理首页
	 *
	 * @return
	 */
	@GetMapping()
	@RequiresPermissions("system:area:area")
	String Area(){
	    return prefix + "/area";
	}

	/**
	 * 获取地区管理列表
	 *
	 * @return
	 */
	@ApiOperation(value="获取地区列表", notes="") // @ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”
	@ResponseBody
	@GetMapping("/list/{pId}")
	@RequiresPermissions("system:area:area")
	public List<AreaDO> list(@PathVariable("pId") String pId){
		//查询列表数据
		Map<String, Object> query = new HashMap<>(16);
		query.put("parentId",pId);
		List<AreaDO> areaList = areaService.list(query);
		return areaList;
	}

	/**
	 * 点击新增跳转到新增页面
	 *
	 * @param pId
	 * @param model
	 * @return
	 */
	@GetMapping("/add/{pId}")
	@RequiresPermissions("system:area:add")
	String add(@PathVariable("pId") String pId, Model model) {
		model.addAttribute("pId", pId);
		if (Constant.AREA_ROOT_ID.equals(pId)) {
			model.addAttribute("pName", "中国");
		} else {
			model.addAttribute("pName", areaService.get(pId).getName());
		}
		return  prefix + "/add";
	}

	/**
	 * 点击修改调转到修改页面
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:area:edit")
	String edit(@PathVariable("id") String id, Model model) {
		AreaDO area = areaService.get(id);
		model.addAttribute("area", area);
		if(Constant.AREA_ROOT_ID.equals(area.getParentId())) {
			model.addAttribute("parentAreaName", "无");
		}else {
			AreaDO parArea = areaService.get(area.getParentId());
			model.addAttribute("parentAreaName", parArea.getName());
		}
		return  prefix + "/edit";
	}
	
	/**
	 * 新增-保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:area:add")
	public R save(AreaDO area){
		AreaDO areaDO = areaService.saveArea(area);
		if(areaDO != null && StringUtils.isNotBlank(areaDO.getId())){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改-保存
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:area:edit")
	public R update( AreaDO area){
		AreaDO areaDO = areaService.updateArea(area);
		if(areaDO != null && StringUtils.isNotBlank(areaDO.getId())){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除单个
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:area:remove")
	public R remove( String id){
		if(areaService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除批量
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:area:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		areaService.batchRemove(ids);
		return R.ok();
	}

	/**
	 * 获取地区树
	 *
	 * @return
	 */
	@GetMapping("/tree")
	@ResponseBody
	public Tree<AreaDO> tree() {
		Tree<AreaDO> tree = new Tree<AreaDO>();
		tree = areaService.getTree();
		return tree;
	}

	@GetMapping("/treeView")
	String treeView() {
		return  prefix + "/areaTree";
	}
	
}
